<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.ExchangegoodsTool");


class ExchangegoodsAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	
	/**
	 * 积分商城商品列表
	 * Enter description here ...
	 */
	public function exchangegoods_list(){
	
	    /* 取得过滤条件 */	
	    /*$sort_flag  = sort_flag($goods_list['filter']);
	    $this->assign($sort_flag['tag'], $sort_flag['img']); // 默认排序字段*/
		
	    $sql_goods_list 		= 	get_sql_exchange_goodslist();
	    $param = array(
				'result'	=> $sql_goods_list,				//分页用的数组或sql
				'listvar'	=> 'goods_list',				//分页循环变量
				'listRows'	=> 10,							//每页记录数
				//'parameter'=>'cat_id=' . $cat_id . "&intro_type=" . $intro_type . "&invalid=" . $invalid,//url分页后继续带的参数
				'target'	=> 'content',	    			//ajax更新内容的容器id，不带#
				'pagesId'	=> 'page',						//分页后页的容器id不带# target和pagesId同时定义才Ajax分页
				'template'	=> 'Exchangegoods:exchange_goods_list',		//ajax更新模板
		);		 
		$this->page_exchangegoods_list($param);
		
		$this->display('exchange_goods_list');
	}
	
	/**
	 * 积分商城商品列表分页
	 * Enter description here ...
	 */
	public function page_exchangegoods_list($param){
		extract($param);
		//总记录数
		$flag = is_string($result);
		
		$listvar = $listvar ? $listvar : 'list';
		$listRows = $listRows? $listRows : 21;
		if ($flag){
			$totalRows	=	count(M()->query($result));
		}			
		else{
			$totalRows = ($result) ? count($result) : 1;
		}
		//创建分页对象
		if ($target && $pagesId)
			$p = new Page($totalRows, $listRows, $parameter, $url,$target, $pagesId);
		else
			$p = new Page($totalRows, $listRows, $parameter,$url);
		//抽取数据
		if ($flag) {
			$result .= " LIMIT {$p->firstRow},{$p->listRows}";
			$voList = M()->query($result);
		} else {
			$voList = array_slice($result, $p->firstRow, $p->listRows);
		}
		$pages = C('PAGE');//要ajax分页配置PAGE中必须theme带%ajax%，其他字符串替换统一在配置文件中设置，
		
		//可以使用该方法前用C临时改变配置
		foreach ($pages as $key => $value) {
			$p->setConfig($key, $value); // 'theme'=>'%upPage% %linkPage% %downPage% %ajax%'; 要带 %ajax%
		}
		//分页显示
		$page = $p->show();		
		//模板赋值
		$this->assign($listvar, $voList);
		$this->assign("page", $page);
		if ($this->isAjax()) {//判断ajax请求
			layout(false);
			$template = (!$template) ? 'ajaxlist' : $template;
			exit($this->fetch($template));
		}
		return $voList;
	}
	
	
	/**
	 * 添加积分商城商品
	 * Enter description here ...
	 */
	public function add_exchangegoods(){
	
	    /*初始化*/
	    $goods 					= 	array();
	    $goods['is_exchange'] 	= 	1;
	    $goods['is_hot']      	= 	0;
	    $goods['option']      	= 	'<option value="0">' . L('make_option') . '</option>';
	
	    $this->assign('goods',       	$goods);
	    $this->assign('form_action', 	'insert');
	    $this->assign('action', 		'add_exchangegoods_ok');	    	
		
		$this->display('exchange_goods_info');
	}
	
	
	/**
	 * 处理添加积分商城商品
	 * Enter description here ...
	 */
	public function add_exchangegoods_ok(){
		import("@.Plugs.Exchange");
		
		$exc   				= 	new exchange("ybr_exchange_goods", null, 'goods_id', 'exchange_integral');
	    /*检查是否重复*/
	    $is_only	 		= 	$exc->is_only('goods_id', $_POST['goods_id'],0, " goods_id ='$_POST[goods_id]'");
	
	    if (!$is_only){
	        //sys_msg($_LANG['goods_exist'], 1);
	        $this->error(L('goods_exist'));
	    }
	
	    /*插入数据*/
	    $add_time 				= 	gmtime();
	    if (empty($_POST['goods_id'])){
	        $_POST['goods_id'] 	=	0;
	    }
	    $sql 					= 	"INSERT INTO " . 'ybr_exchange_goods' . "(goods_id, exchange_integral, is_exchange, is_hot) ".
	            					"VALUES ('$_POST[goods_id]', '$_POST[exchange_integral]', '$_POST[is_exchange]', '$_POST[is_hot]')";
	    M()->query($sql);
		
	    admin_log($_POST['goods_id'],'add','exchange_goods'); // 记录操作
	
	    /*clear_cache_files(); // 清除相关的缓存文件*/
	
		$this->success("操作成功",U('exchangegoods/exchangegoods_list'));
	}
		
	/**
	 * 修改积分商城商品
	 * Enter description here ...
	 */
	public function mod_exchangegoods(){
	
		$CommonModel	=	new CommonModel();
	    /* 取商品数据 */
	    $sql 			= 	"SELECT eg.goods_id, eg.exchange_integral,eg.is_exchange, eg.is_hot, g.goods_name ".
	           				" FROM " . 'ybr_exchange_goods' . " AS eg ".
	           				"  LEFT JOIN " . 'ybr_goods' . " AS g ON g.goods_id = eg.goods_id ".
	           				" WHERE eg.goods_id='$_REQUEST[id]'";
	   	$goods 			= 	$CommonModel->getRow($sql);
	    $goods['option']= 	'<option value="' . $goods['goods_id'].'">' . $goods['goods_name'] . '</option>';
	
	    $this->assign('goods',       	$goods);
	    $this->assign('form_action', 	'update');
	    $this->assign('action', 		'mod_exchangegoods_ok');
	
	    $this->display('exchange_goods_info');
	}

	/**
	 * 处理修改积分商城
	 * Enter description here ...
	 */
	public function mod_exchangegoods_ok(){
		import("@.Plugs.Exchange");
		
		$exc   					= 	new exchange("ybr_exchange_goods", null, 'goods_id', 'exchange_integral');
	    
	    if (empty($_POST['goods_id'])){
	        $_POST['goods_id'] 	= 	0;
	    }
	
	    if ($exc->edit("exchange_integral='$_POST[exchange_integral]', is_exchange='$_POST[is_exchange]', is_hot='$_POST[is_hot]' ", $_POST['goods_id'])){
	    	
	        admin_log($_POST['goods_id'], 'edit', 'exchange_goods'); // 记录操作	
	        /*clear_cache_files(); // 清楚缓存*/
	        $this->success( "操作成功",U('exchangegoods/exchangegoods_list') );
	    }
	    else{
	        //die($db->error());
	        $this->error( "操作失败",U('exchangegoods/exchangegoods_list') );
	    }
	}
	
	public function remove_exchangegoods(){
		import("@.Plugs.Exchange");
		
		$exc   					= 	new exchange("ybr_exchange_goods", null, 'goods_id', 'exchange_integral');		
	    $id 					= 	intval($_GET['id']);
	    if ($exc->drop($id)){
	    	
	        admin_log($id,'remove','article');
	        //clear_cache_files(); // 清楚缓存
	    }	
	    $this->success("操作成功",U('exchangegoods/exchangegoods_list'));		
	}
	
	
	/**
	 * 搜索商品
	 * Enter description here ...
	 */
	public function search_goods(){
		
	    $filters 		= 	$_GET['JSON'];	
	    $arr 			= 	get_goods_list($filters);
	
		$return['content']	=	$arr;
		$return['error']	=	0;
		$return['message']	=	"";
	   	$this->ajaxReturn($return);
	}
	
	
	
}










